package org.huawei.view;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import org.huawei.controller.UserController;

public class LoginView extends Application {

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("作业管理系统");

        // 创建布局
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setVgap(25);
        grid.setHgap(25);
        grid.setStyle("-fx-background-color: #fffefe;");

        // 设置背景图片
        Image bgImage = new Image(getClass().getResource("/image/background.jpg").toExternalForm());
        BackgroundImage backgroundImage = new BackgroundImage(bgImage,
                BackgroundRepeat.NO_REPEAT,
                BackgroundRepeat.NO_REPEAT,
                BackgroundPosition.CENTER,
                new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, true, true));

        grid.setBackground(new Background(backgroundImage));
        grid.setStyle("-fx-opacity: 1.0;");

        // 添加图片
        Image image = new Image(getClass().getResource("/image/logo.jpg").toExternalForm());
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(60);
        imageView.setPreserveRatio(true);
        // 将图片添加到布局的第一行
        grid.add(imageView, 1, 0, 4, 1);

        Label pwLabel2 = new Label("作业管理系统");
        pwLabel2.setStyle("-fx-font-size: 32px; -fx-text-fill: rgb(255,254,254); -fx-font-weight: bold;");
        grid.add(pwLabel2, 2, 0);

        Label userLabel = new Label("账户：");
        userLabel.setStyle("-fx-font-size: 20px;");
        grid.add(userLabel, 1, 1);

        TextField userTextField = new TextField();
        userTextField.setPrefWidth(130);
        userTextField.setPrefHeight(25);
        grid.add(userTextField, 2, 1);

        Label pwLabel = new Label("密码：");
        pwLabel.setStyle("-fx-font-size: 20px;");
        grid.add(pwLabel, 1, 2);

        PasswordField pwBox = new PasswordField();
        pwBox.setPrefWidth(130);
        pwBox.setPrefHeight(25);
        grid.add(pwBox, 2, 2);

        Button loginButton = new Button("登录！");
        loginButton.setPrefWidth(120);
        loginButton.setPrefHeight(30);
        loginButton.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white;-fx-font-size: 14px;");

        grid.add(loginButton, 2, 3);

        // 设置场景
        Scene scene = new Scene(grid, 750, 450);
        primaryStage.setScene(scene);

        // Controller
        UserController userController = new UserController(primaryStage);

        // 使用 Lambda 表达式处理事件
        loginButton.setOnAction(e -> userController.trylogin(userTextField.getText(), pwBox.getText()));

        primaryStage.show();
    }

}